home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODF-Interest Archive / July 96 / Notes from the trenches FW_CSt < prev    next >
Encoding:
Internet Message Format  |  1996-08-01  |  5.1 KB  |  [TEXT/ttxt]

  1. Subject:     Notes from the trenches: FW_CStrings
  2. Sent:        7/24/96 12:22 PM
  3. Received:    7/24/96 12:31 PM
  4. From:        Kirk Swenson, kswenson@keypress.com
  5. Reply-To:    ODF Interest, ODF-Interest@CILabs.ORG
  6. To:          OpenDoc Development Framework Discussion List, ODF-Interest@CILabs.
  7.  
  8. Having just spent time debugging in the bowels of my part's and ODF's
  9. string-handling code, I thought I would share a few of the lessons I've
  10. learned for those that have not already made the journey.  (ODF team: feel
  11. free to clarify anything I say unclearly or incorrectly.)  What follows are
  12. my top ten things to remember about dealing with ODF's FW_CString classes,
  13. in RLO (Reverse Letterman Order):
  14.  
  15. 1) Conditionally include <PRStrRep.h> while debugging.
  16.  
  17. FW_CString relies on an opaque FW_SPrivStringRep structure which is not
  18. accessible by the debugger by default.  While opaque structures may be a
  19. good thing in design, they're a royal pain when debugging.  Including the
  20. above header file makes the structure of the opaque FW_SPrivStringRep
  21. available to the debugger.
  22.  
  23. 2) Don't rely on the <PRStrRep.h> file for anything other than debugging.
  24.  
  25. Wrapping the include inside #ifdef FW_DEBUG/#endif will help here, assuming
  26. that you occasionally build a release version of your code. ;-)
  27.  
  28. 3) FW_ODITextParams     fParams;
  29.  
  30. This structure is part of the FW_SPrivStringRep and contains the actual
  31. pointer to the string data.  There is a pointer to the start of the string
  32. data, a length word, and a capacity word indicating the total size of the
  33. buffer.  This is how you figure out what the string actually holds in the
  34. debugger.  Also noteworthy, the fTextStart, fTextByteLength, and
  35. fTextByteCapacity fields of fParams appear to be redundant with the
  36. _buffer, _length, and _maximum fields of the fText (ODIText) structure that
  37. is also contained within the FW_SPrivStringRep -- the only difference is
  38. that the fText fields include the text format word (discussed below) while
  39. the fParams fields do not.
  40.  
  41. 4) The string data start at buffer[4].
  42.  
  43. String buffers are allocated to be 4 bytes larger than the space required
  44. for the characters, because the first four bytes are a longword which
  45. represents the text format (ODITextFormat), and is required for
  46. interoperability with OpenDoc text-handling routines.  For example, the
  47. buffer for a FW_CString32 is actually 36 bytes.  According to the
  48. documentation, the only currently supported value for this word is
  49. kODTraditionalMacText, which has a value of 0.  The pointer in fParams
  50. points to the actual start of the text, not of the buffer.
  51.  
  52. 5) The size of the FW_SPrivStringRep structure is 40 bytes.
  53.  
  54. If you have a lot of unique strings, particularly if they're generally
  55. small, this is a lot of overhead.  As strings are reused, however, the
  56. overhead gets amortized across multiple strings.  It would appear that this
  57. overhead could be reduced by removing the redundancy within the
  58. FW_SPrivStringRep, at the expense of requiring some additional pointer
  59. manipulations, but I may be missing something here.
  60.  
  61. 6) The FW_SPrivStringRep structure is allocated on the heap.
  62.  
  63. Unique FW_CStrings require two heap allocations (one for the
  64. FW_SPrivStringRep and one for the buffer), while unique bounded strings
  65. require one.  Setting a string equal to another string, of course, will
  66. reuse the other's representation, and hence will require no heap
  67. allocations.
  68.  
  69. 7) The size of a FW_CString is only 8 bytes.
  70.  
  71. FW_CString is a very lightweight class, particularly when it can refer to
  72. other representations and doesn't have to allocate any storage of its own.
  73. This suggests that code that generally refers to other strings without
  74. changing them should use FW_CString rather than a bounded string.
  75.  
  76. 8) Bounded strings don't necessarily keep their strings in their buffers.
  77.  
  78. When debugging bounded strings (FW_CString32, FW_CString255), particularly
  79. without being able to look into the opaque FW_SPrivStringRep, it is
  80. tempting to believe that the string data should be in the static buffer
  81. allocated by the bounded string.  Due to its reference-counting nature,
  82. however, a bounded string initialized from another string will refer to
  83. that other string's representation.  It will only use its own buffer if it
  84. has to do so to store a unique string.  You have to follow the pointer in
  85. fParams to find the string.
  86.  
  87. 9) Bounded strings aren't limited to the size of their bounds.
  88.  
  89. As suggested by the previous item, the buffer allocated by a bounded string
  90. is to be used if necessary or convenient, but there is no requirement that
  91. it be used.  If a bounded string grows to be longer than 32 or 255, it's
  92. quite happy to re-allocate a larger buffer.  So if you have one 33-byte
  93. string among many shorter strings, you don't have to worry about truncating
  94. the long string.
  95.  
  96. 10) There is a bug in ODF's bounded string code.
  97.  
  98. As detailed in my previous message, there is a bug in ODF R1 which rears
  99. its head when modifying a (unique) bounded string that has previously been
  100. assigned to another string.  Using FW_CString instead of FW_CString32 or
  101. FW_CString255 is a workaround until there is a fix.
  102.  
  103. Kirk Swenson
  104. Senior Software Engineer
  105. Key Curriculum Press
  106. kswenson@keypress.com
  107.  
  108.  
  109.